<?php
/*
---------------------------------------------------------
														+
名称:易通支付通道										+
														+
控制器名称:YiTongController								+
														+
日期:2017-10-11											+
														+
作者:小翁												+
														+
---------------------------------------------------------
*/
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

class JunanController extends PayController{
    protected $config;
    protected $key;
    protected $url;
    protected $class;

    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $ismobile = isMobile();
        $return = $this->orderadd( $parameter );

        switch($this->config['paytype']){
            //微信
            case 1:
                //微信H5
            case 2:
                $pay_bankcode = 'WXZF';
                $tongdao = 'NJWXH5D1';
                break;
            //支付宝扫码
            case 3:
            case 4:
            case 17:
            case 18:
                $pay_bankcode = 'ALIPAY';
                $tongdao = 'XTFAH5D0';
                break;

            default:
                $this->showmessage('不支持此产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $requestarray = array(
            "pay_memberid" => $this->config['mch_id'],
            "pay_orderid" => $return['orderid'],
            "pay_amount" => $return['amount'],
            "pay_applydate" => date("Y-m-d H:i:s"),
            "pay_bankcode" => $pay_bankcode,
            "pay_notifyurl" => $return['notifyurl'],
            "pay_callbackurl" => $return['callbackurl']
        );
        ksort($requestarray);                               //ASCII码排序
        reset($requestarray);                               //定位到第一个下标
        $md5str = "";
        foreach ($requestarray as $key => $val) {
            $md5str = $md5str.$key."=>".$val."&";
        }
        $sign = strtoupper(md5($md5str."key=".$this->config['signkey']));
        $requestarray["pay_md5sign"] = $sign;

        $requestarray["tongdao"] = $tongdao;
        $requestarray["return_type"] = 1;
        $requestarray["pay_requestIp"] = getIP();

        $res = curl($this->config['gateway'], httpstr($requestarray));//curl_post
        $resp = json_decode($res,true);

        $code = $resp['pay_url'];
        if(!$code){
            $this->showmessage('未获取到支付链接!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $paytype = transArr( C("PAYTYPES"), "id");
        $this->assign("wap_url", $code);
        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);
        $this->assign("imgurl", C("website.DOMAIN")."/Pay_Qrcode_getCode?type=2&data=" . base64_encode($code));

        switch($this->config['paytype']){
            //微信
            case 1:
                //微信H5
            case 2:
                if($ismobile){
                    header("location: {$code}"); exit;
                    //$this->display("WeiXin/weixinwap");
                }else{
                    $this->display("WeiXin/weixin");
                }
                break;
            //支付宝扫码
            case 3:
            case 4:
            case 17:
            case 18:
                if($ismobile){
                    header("location: {$code}"); exit;
                    // $this->display("WeiXin/alipaywap");
                }else{
                    $this->display("WeiXin/alipay");
                }
                break;
            default:
                $this->display("WeiXin/Pay");
        }
    }

    //通知
    public function notifyurl(){
        //$this->debug_callback();

        if(!$this->checkip(getIP(),['47.244.169.226'])){
            exit("wrong");
        }

        $p = I('get.');

        //获取订单信息
        $orderid = $p['orderid'];
        $find = $this->checkOrderNotify($orderid);

        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

            "pay_orderid_rd"=>'orderid'));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $sign = $p['sign'];
        unset($p['sign']);

        ksort($p);                               //ASCII码排序
        reset($p);                               //定位到第一个下标
        $md5str = "";
        foreach ($p as $key => $val) {
            $md5str = $md5str.$key."=>".$val."&";
        }
        $sign1 = strtoupper(md5($md5str."key=".$this->config['signkey']));

        if($sign1 !== $sign ){
            exit("sign_wrong");
        }

        if($p['returncode'] !== '00'){
            exit("pay_fail");
        }

        $this->async($find['pay_orderid'],'ok');
    }

    public function callBackUrl(){

        echo '支付成功'; exit;
    }
}